package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Models.Brew;
import Models.DryHopNotes;
import Models.FermentationNotes;
import Models.PitchNotes;

public class BrewPresenter extends AbstractPresenter<Brew> {
	final String SINGLEITEM = "brews";
	final String MULTIITEM = "brewss";
	
	public ArrayList<Brew> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public Brew tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public Brew createItemFromJSONObject(JSONObject jsonObject){
		Brew item = new Brew();
		try{
			item.setBrewID(jsonObject.getInt("bid"));
			item.setBrewPSI(jsonObject.getDouble("bpsi"));
			item.setConditioningFermentationVolume(jsonObject.getDouble("CTVol"));
			item.setConditioningTemp(jsonObject.getDouble("CTTemp"));
			item.setConditioningVesselID(jsonObject.getInt("cvid"));
			item.setGanttColor(jsonObject.getInt("ganttColor"));
			item.setGanttConditioningID(jsonObject.getInt("ganttCID"));
			item.setGanttFGICSV(jsonObject.getString("ganttFGI"));
			item.setGanttFilter(jsonObject.getBoolean("ganttFilter"));
			item.setGanttFine(jsonObject.getBoolean("ganttFine"));
			item.setGanttHarvest(jsonObject.getBoolean("ganttHarvest"));
			item.setGanttPrimaryID(jsonObject.getInt("ganttPID"));
			item.setGanttSecondaryID(jsonObject.getInt("ganttSID"));
			item.setOrigBrew(jsonObject.getInt("origBrew"));
			item.setPrimaryVesselID(jsonObject.getInt("pfvid"));
			item.setSecondaryFermentationNotesGrav(jsonObject.getDouble("SFGrav01"));
			item.setSecondaryFermentationNotesNote(jsonObject.getString("SFNote01"));
			item.setSecondaryFermentationNotesTemp(jsonObject.getDouble("SFTemp01"));
			item.setSecondaryFermentationVolume(jsonObject.getDouble("SFVol01"));
			item.setSecondaryVesselID(jsonObject.getInt("sfvid"));
			item.setState(jsonObject.getInt("BState"));
			item.setTerminalGravity(jsonObject.getDouble("btg"));
			item.setVersion(jsonObject.getInt("version"));
			item.setVolumeLeft(jsonObject.getDouble("volLeft"));
			
			if(!jsonObject.isNull("BDate")){
				item.setBrewDate(timeFormat.parse(jsonObject.getString("BDate")));
			} else {
				item.setBrewDate(null);
			}
			if(!jsonObject.isNull("ganttStart")){
				item.setGanttStart(timeFormat.parse(jsonObject.getString("ganttStart")));
			} else {
				item.setGanttStart(null);
			}
			if(!jsonObject.isNull("ganttSecond")){
				item.setGanttSecond(timeFormat.parse(jsonObject.getString("ganttSecond")));
			} else {
				item.setGanttSecond(null);
			}
			if(!jsonObject.isNull("ganttCond")){
				item.setGanttCond(timeFormat.parse(jsonObject.getString("ganttCond")));
			} else {
				item.setGanttCond(null);
			}
			if(!jsonObject.isNull("ganttStop")){
				item.setGanttStop(timeFormat.parse(jsonObject.getString("ganttStop")));
			} else {
				item.setGanttStop(null);
			}
			if(!jsonObject.isNull("ganttHarvestDate")){
				item.setGanttHarvestDate(timeFormat.parse(jsonObject.getString("ganttHarvestDate")));
			} else {
				item.setGanttHarvestDate(null);
			}
			if(!jsonObject.isNull("CTDate")){
				item.setConditioningDate(timeFormat.parse(jsonObject.getString("CTDate")));
			} else {
				item.setConditioningDate(null);
			}
			if(!jsonObject.isNull("CTTime")){
				item.setConditioningTime(timeFormat.parse(jsonObject.getString("CTTime")));
			} else {
				item.setConditioningTime(null);
			}
			if(!jsonObject.isNull("PDate")){
				item.setPackagingDate(timeFormat.parse(jsonObject.getString("PDate")));
			} else {
				item.setPackagingDate(null);
			}
			if(!jsonObject.isNull("PTime")){
				item.setPackagingTime(timeFormat.parse(jsonObject.getString("PTime")));
			} else {
				item.setPackagingTime(null);
			}
			if(!jsonObject.isNull("SFDate01")){
				item.setSecondaryFermentationNotesDate(timeFormat.parse(jsonObject.getString("SFDate01")));
			} else {
				item.setSecondaryFermentationNotesDate(null);
			}
			if(!jsonObject.isNull("SFTime01")){
				item.setSecondaryFermentationNotesTime(timeFormat.parse(jsonObject.getString("SFTime01")));
			} else {
				item.setSecondaryFermentationNotesTime(null);
			}
			
			final BeerViewPresenter bvPresenter = new BeerViewPresenter();
			JSONObject jsonGroup1 = jsonObject.getJSONObject("BBeerID");
			item.setBeer(bvPresenter.createItemFromJSONObject(jsonGroup1));
			
			final RecipePresenter recipePresenter = new RecipePresenter();
			JSONObject jsonGroup2 = jsonObject.getJSONObject("rid");
			item.setRecipe(recipePresenter.createItemFromJSONObject(jsonGroup2));

			final BrewNotePresenter presenter = new BrewNotePresenter();
			if(!jsonObject.isNull("brewNoteID")){
				//create product if set
				JSONObject jsonGroup = jsonObject.getJSONObject("brewNoteID");
				item.setBrewNoteID1(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setBrewNoteID1(null);
			}
			if(!jsonObject.isNull("brewNoteID2")){
				//create product if set
				JSONObject jsonGroup = jsonObject.getJSONObject("brewNoteID2");
				item.setBrewNoteID2(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setBrewNoteID2(null);
			}
			if(!jsonObject.isNull("brewNoteID3")){
				//create product if set
				JSONObject jsonGroup = jsonObject.getJSONObject("brewNoteID3");
				item.setBrewNoteID3(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setBrewNoteID3(null);
			}
			if(!jsonObject.isNull("brewNoteID4")){
				//create product if set
				JSONObject jsonGroup = jsonObject.getJSONObject("brewNoteID4");
				item.setBrewNoteID4(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setBrewNoteID4(null);
			}
			if(!jsonObject.isNull("brewNoteID5")){
				//create product if set
				JSONObject jsonGroup = jsonObject.getJSONObject("brewNoteID5");
				item.setBrewNoteID5(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setBrewNoteID5(null);
			}
			if(!jsonObject.isNull("brewNoteID6")){
				//create product if set
				JSONObject jsonGroup = jsonObject.getJSONObject("brewNoteID6");
				item.setBrewNoteID6(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setBrewNoteID6(null);
			}
			
			//Set DryHopNotes Collection
			ArrayList<DryHopNotes> dryHops = new ArrayList<DryHopNotes>();
			if(!jsonObject.isNull("dryHopNoteCollection")){
				Object itm = jsonObject.get("dryHopNoteCollection");
				final DryHopNotesPresenter dpresenter = new DryHopNotesPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						dryHops.add(dpresenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					dryHops.add(dpresenter.createItemFromJSONObject(jsonItem));
				}
				item.setDryHopNotes(dryHops);
			}
			
			//Set FermentationNotes Collection
			ArrayList<FermentationNotes> fermNotes = new ArrayList<FermentationNotes>();
			if(!jsonObject.isNull("fermentationNoteCollection")){
				Object itm = jsonObject.get("fermentationNoteCollection");
				final FermentationNotesPresenter fpresenter = new FermentationNotesPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						fermNotes.add(fpresenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					fermNotes.add(fpresenter.createItemFromJSONObject(jsonItem));
				}
				item.setFermNotes(fermNotes);
			}
			
			//Set Documents Collection
			ArrayList<PitchNotes> pitchNotes = new ArrayList<PitchNotes>();
			if(!jsonObject.isNull("pitchNoteCollection")){
				Object itm = jsonObject.get("pitchNoteCollection");
				final PitchNotesPresenter ppresenter = new PitchNotesPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						pitchNotes.add(ppresenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					pitchNotes.add(ppresenter.createItemFromJSONObject(jsonItem));
				}
				item.setPitchNotes(pitchNotes);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(Brew item){
		try{
			JSONObject json = new JSONObject();
			
			json.put("bid", item.getBrewID());
			json.put("bpsi", item.getBrewPSI());
			json.put("CTVol", item.getConditioningFermentationVolume());
			json.put("CTTemp", item.getConditioningTemp());
			json.put("cvid", item.getConditioningVesselID());
			json.put("ganttColor", item.getGanttColor());
			json.put("ganttCID", item.getGanttConditioningID());
			json.put("ganttFGI", item.getGanttFGICSV());
			json.put("ganttFilter", item.isGanttFilter());
			json.put("ganttFine", item.isGanttFine());
			json.put("ganttHarvest", item.isGanttHarvest());
			json.put("ganttPID", item.getGanttPrimaryID());
			json.put("ganttSID", item.getGanttSecondaryID());
			json.put("origBrew", item.getOrigBrew());
			json.put("pfvid", item.getPrimaryVesselID());
			json.put("SFGrav01", item.getSecondaryFermentationNotesGrav());
			json.put("SFNote01", item.getSecondaryFermentationNotesNote());
			json.put("SFTemp01", item.getSecondaryFermentationNotesTemp());
			json.put("SFVol01", item.getSecondaryFermentationVolume());
			json.put("sfvid", item.getSecondaryVesselID());
			json.put("BState", item.getState());
			json.put("btg", item.getTerminalGravity());
			json.put("version", item.getVersion());
			json.put("volLeft", item.getVolumeLeft());
			
			if(item.getBrewDate()!=null){
				json.put("BDate", getDate(item.getBrewDate()));
			} else {
				//json.put("BDate", JSONObject.NULL);
			}
			if(item.getGanttStart()!=null){
				json.put("ganttStart", getDate(item.getGanttStart()));
			} else {
				//json.put("ganttStart", JSONObject.NULL);
			}
			if(item.getGanttSecond()!=null){
				json.put("ganttSecond", getDate(item.getGanttSecond()));
			} else {
				//json.put("ganttSecond", JSONObject.NULL);
			}
			if(item.getGanttCond()!=null){
				json.put("ganttCond", getDate(item.getGanttCond()));
			} else {
				//json.put("ganttCond", JSONObject.NULL);
			}
			if(item.getGanttStop()!=null){
				json.put("ganttStop", getDate(item.getGanttStop()));
			} else {
				//json.put("ganttStop", JSONObject.NULL);
			}
			if(item.getGanttHarvestDate()!=null){
				json.put("ganttHarvestDate", getDate(item.getGanttHarvestDate()));
			} else {
				//json.put("ganttHarvestDate", JSONObject.NULL);
			}
			if(item.getConditioningDate()!=null){
				json.put("CTDate", getDate(item.getConditioningDate()));
			} else {
				//json.put("CTDate", JSONObject.NULL);
			}
			if(item.getConditioningTime()!=null){
				json.put("CTTime", getDate(item.getConditioningTime()));
			} else {
				//json.put("CTTime", JSONObject.NULL);
			}
			if(item.getPackagingDate()!=null){
				json.put("PDate", getDate(item.getPackagingDate()));
			} else {
				//json.put("PDate", JSONObject.NULL);
			}
			if(item.getPackagingTime()!=null){
				json.put("PTime", getDate(item.getPackagingTime()));
			} else {
				//json.put("PTime", JSONObject.NULL);
			}
			if(item.getSecondaryFermentationNotesDate()!=null){
				json.put("SFDate01", getDate(item.getSecondaryFermentationNotesDate()));
			} else {
				//json.put("SFDate01", JSONObject.NULL);
			}
			if(item.getSecondaryFermentationNotesTime()!=null){
				json.put("SFTime01", getDate(item.getSecondaryFermentationNotesTime()));
			} else {
				//json.put("SFTime01", JSONObject.NULL);
			}

			final BeerViewPresenter bvPresenter = new BeerViewPresenter();
			json.put("BBeerID", bvPresenter.createJSON(item.getBeer()));
			
			final RecipePresenter recipePresenter = new RecipePresenter();
			json.put("rid", recipePresenter.createJSON(item.getRecipe()));

			final BrewNotePresenter presenter = new BrewNotePresenter();
			if(item.getBrewNoteID1()!=null){
				json.put("brewNoteID", presenter.createJSON(item.getBrewNoteID1()));
			} else {
				//json.put("brewNoteID", JSONObject.NULL);
			}
			if(item.getBrewNoteID2()!=null){
				json.put("brewNoteID2", presenter.createJSON(item.getBrewNoteID2()));
			} else {
				//json.put("brewNoteID2", JSONObject.NULL);
			}
			if(item.getBrewNoteID3()!=null){
				json.put("brewNoteID3", presenter.createJSON(item.getBrewNoteID3()));
			} else {
				//json.put("brewNoteID3", JSONObject.NULL);
			}
			if(item.getBrewNoteID4()!=null){
				json.put("brewNoteID4", presenter.createJSON(item.getBrewNoteID4()));
			} else {
				//json.put("brewNoteID4", JSONObject.NULL);
			}
			if(item.getBrewNoteID5()!=null){
				json.put("brewNoteID5", presenter.createJSON(item.getBrewNoteID5()));
			} else {
				//json.put("brewNoteID5", JSONObject.NULL);
			}
			if(item.getBrewNoteID6()!=null){
				json.put("brewNoteID6", presenter.createJSON(item.getBrewNoteID6()));
			} else {
				//json.put("brewNoteID6", JSONObject.NULL);
			}
			
			//Create Dry Hop Notes Collection
			if(item.getDryHopNotes()!=null){
				DryHopNotesPresenter dhpresenter = new DryHopNotesPresenter();
				if(item.getDryHopNotes().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getDryHopNotes().size(); i++){
						jsonArray.put(dhpresenter.createJSON(item.getDryHopNotes().get(i)));
					}
					json.put("dryHopNoteCollection", jsonArray);
				}
			}
			
			//Create Doc Collection
			if(item.getFermNotes()!=null){
				FermentationNotesPresenter fpresenter = new FermentationNotesPresenter();
				if(item.getFermNotes().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getFermNotes().size(); i++){
						jsonArray.put(fpresenter.createJSON(item.getFermNotes().get(i)));
					}
					json.put("fermentationNoteCollection", jsonArray);
				}
			}
			
			//Create Doc Collection
			if(item.getPitchNotes()!=null){
				PitchNotesPresenter pnpresenter = new PitchNotesPresenter();
				if(item.getPitchNotes().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getPitchNotes().size(); i++){
						jsonArray.put(pnpresenter.createJSON(item.getPitchNotes().get(i)));
					}
					json.put("pitchNoteCollection", jsonArray);
				}
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Brew getEmptyEntity(){
		Brew entity = new Brew();
		return entity;
	}
}
